library common;

uses
  sysutils, windows, Classes, Inifiles, TntSysUtils,
  WideIniFiles in '../../WideIniFiles.pas',
  header in '../../header.pas';

function SecToTime(sec: integer): Pointer;stdcall;
var min: Word;
    s: Byte;
    t_min, t_s: string;
begin
{$R-}
  min := sec div 60;
  s   := sec - min * 60;
{$R+}
  t_min := IntToStr(min);
  if s < 10 then
    t_s := '0' + IntToStr(s)
  else
    t_s := IntToStr(s);

  Result := PChar(t_min + ':' + t_s);
end;

function Imp(Imp_file: Pointer; var imps: TExp_struct): Boolean;stdcall;
var
  i, c: integer;
  pt: WideString;
  pls: TWideIniFile;
begin
  Result := True;

  pt := WideExtractFileExt(PWideChar(Imp_file));

  if WideSameStr(pt, '.pls') then
    begin
      pls := TWideIniFile.Create(PWideChar(Imp_file));
      try
        if pls.ReadInteger('playlist', 'Version', 0) <> 2 then
          Result := false
        else
          begin
            c := pls.ReadInteger('playlist', 'NumberOfEntries', 0);
            SetLength(imps, c);
            for i := 0 to c - 1 do
              begin
                imps[i].fn := UTF8Decode(pls.ReadString('playlist', 'File' + IntToStr(i + 1), ''));
                imps[i].tn := UTF8Decode(pls.ReadString('playlist', 'Title' + IntToStr(i + 1), ''));
                imps[i].tm := pls.ReadInteger('playlist', 'Length' + IntToStr(i + 1), -1);
              end;
          end;
      finally
        pls.Free;
      end;
    end;
end;

function expPLS(Exp_file: Pointer; exps: TExp_struct):Boolean;stdcall;
var
  ListFile: TextFile;
  i: integer;
begin
  Result := True;

  AssignFile(ListFile, PWideChar(Exp_file));
{$I-}
  Rewrite(ListFile);
{$I+}
  if IOResult = 0 then
    begin
      Writeln(ListFile, '[playlist]');
      for i := 0 to Length(exps) - 1 do
         begin
           Writeln(ListFile, 'File' + InttoStr(i + 1) + '=' + UTF8Encode(exps[i].fn));
           Writeln(ListFile, 'Title' + InttoStr(i + 1) + '=' + UTF8Encode(exps[i].tn));
           Writeln(ListFile, 'Length' + InttoStr(i + 1) + '=' + IntToStr(exps[i].tm));
         end;
      Writeln(ListFile, 'NumberOfEntries=' + InttoStr(Length(exps)));
      Writeln(ListFile, 'Version=2');
    end
  else
    begin
      Result := false;
      Exit;
    end;
{$I-}
  CloseFile(ListFile);
{$I+}
end;

function expM3U(Exp_file: Pointer; exps: TExp_struct):Boolean;stdcall;
var
  ListFile: TextFile;
  i: integer;
begin
  Result := True;

  AssignFile(ListFile, PWideChar(Exp_file));
{$I-}
  Rewrite(ListFile);
{$I+}
  if IOResult = 0 then
    begin
      Writeln(ListFile, '#EXTM3U');
      for i := 0 to Length(exps) - 1 do
        begin
          Writeln(ListFile, '#EXTINF:' + IntToStr(exps[i].tm) + ',' + UTF8Encode(exps[i].tn));
          Writeln(ListFile, UTF8Encode(exps[i].fn));
        end;
    end
  else
    begin
      Result := false;
      Exit;
    end;
{$I-}
  CloseFile(ListFile);
{$I+}
end;

function expTXT(Exp_file: Pointer; exps: TExp_struct):Boolean;stdcall;
var
  ListFile: TextFile;
  i: integer;
begin
  Result := True;

  AssignFile(ListFile, PWideChar(Exp_file));
{$I-}
  Rewrite(ListFile);
{$I+}
  if IOResult = 0 then
    for i := 0 to Length(exps) - 1 do
      Writeln(ListFile, UTF8Encode(exps[i].fn))
  else
    begin
      Result := false;
      Exit;
    end;
{$I-}
  CloseFile(ListFile);
{$I+}
end;

function expHTML(Exp_file: Pointer; exps: TExp_struct):Boolean;stdcall;
var ListFile: TextFile;
    i: integer;
begin
  AssignFile(ListFile, PWideChar(Exp_file));
{$I-}
  Rewrite(ListFile);
{$I+}
  if IOResult = 0 then
  begin
    Writeln(ListFile, UTF8Encode('<html>' + #13 + '<head>'));
    Writeln(ListFile, UTF8Encode('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'));
    Writeln(ListFile, UTF8Encode('<style TYPE="text/css">'));
    Writeln(ListFile, UTF8Encode('<!--BODY { background: #2B2651; font-family: verdana, arial;}'));
    Writeln(ListFile, UTF8Encode('.list { font-size: 13px; line-height: 20px; text-align: left; color: #FFAD00; font-weight: bold;}'));
    Writeln(ListFile, UTF8Encode('.title { font-size: 30px; line-height: 40px; text-align: left; color: #82C0FF; }'));
    Writeln(ListFile, UTF8Encode('-->' + #13 + '</style>'));
    Writeln(ListFile, UTF8Encode('<title>SimplyPlay HTML Playlist</title>' + #13 + '</head>'));
    Writeln(ListFile, UTF8Encode('<body>'));
    Writeln(ListFile, UTF8Encode('<div align="left">'));
    Writeln(ListFile, UTF8Encode('<table>'));
    Writeln(ListFile, UTF8Encode('<tr>'));
    Writeln(ListFile, UTF8Encode('<td>' + #13 + '<p class="title">'));
    Writeln(ListFile, UTF8Encode('<nobr>SimplyPlay Media Player</nobr>'));
    Writeln(ListFile, UTF8Encode('</p>' + #13 + '</td>'));
    Writeln(ListFile, UTF8Encode('</tr>'));
    Writeln(ListFile, UTF8Encode('</table>'));
    Writeln(ListFile, UTF8Encode('<hr color="#FFAD00" size=1 align="left" width=400>'));
    Writeln(ListFile, UTF8Encode('<hr style="margin-left: 180px" color="#FFAD00" size=1 align="left" width=800>'));
    Writeln(ListFile, UTF8Encode('<hr style="margin-left: 300px" color="#FFAD00" size=1 align="left" width=400>'));
    Writeln(ListFile, UTF8Encode('</div>'));
    Writeln(ListFile, UTF8Encode('<div style="margin-left: 310px" align="left">'));
    Writeln(ListFile, UTF8Encode('<table>'));
    Writeln(ListFile, UTF8Encode('<tr>'));
    Writeln(ListFile, UTF8Encode('<td>' + #13 + '<p class="list">'));

    for i := 0 to Length(exps) - 1 do
      begin
        Write(ListFile, UTF8Encode('<nobr>' + InttoStr(i + 1) + '. ' + exps[i].tn));
        if exps[i].tm > 0 then
          Write(ListFile, UTF8Encode(' - ' + PChar(SecToTime(exps[i].tm))));
        Writeln(ListFile, UTF8Encode('</nobr><br />'));
      end;
      
    Writeln(ListFile, UTF8Encode('</p>' + #13 + '</td>'));
    Writeln(ListFile, UTF8Encode('</tr>'));
    Writeln(ListFile, UTF8Encode('</table>'));
    Writeln(ListFile, UTF8Encode('</div>'));
    Writeln(ListFile, UTF8Encode('<hr color="#FFAD00" size=1 align="left" width=300>'));
    Writeln(ListFile, UTF8Encode('<span style="color: #FFAD00; font-size: 9px;">'));
    Writeln(ListFile, UTF8Encode('SimplyPlay generated playlist ' + DateToStr(Date) + '</span>'));
    Writeln(ListFile, UTF8Encode('</body>' + #13 + '</html>'));
  end
  else
  begin
    Result := false;
    Exit;
  end;
{$I-}
  CloseFile(ListFile);
{$I+}
  if IOResult = 0 then Result := true else Result := false;
end;

function Delete_File(w_handle: HWND; file_n: Pointer): Boolean;stdcall;
begin
  Result := True;
  if MessageBox(w_handle, 'Do you realy want to delete this file?', '', MB_YESNO) = idyes
  then
  try
    DeleteFileW(file_n);
  except
    Result := False;
  end
  else Result := False;
end;

exports Imp, expPLS, expM3U, expTXT, expHTML, SecToTime, Delete_File;

begin
end.
